import { HandlerUpperCase } from "./handle_upper_case";
import { HandlerLowerCase } from "./handler_lower_case";
import { HandlerTitleCase } from "./handler_title_case";
import { HandlerSnakeCaseToHttpHeader } from "./handler_snake_case_to_http_header";
import { HandlerHttpHeaderToSnakeCase } from "./handler_http_header_to_snake_case";
import { HandlerSnakeCaseToCameraCase } from "./handler_snake_to_camera_case";
import { HandlerCameraCaseToSnakeCase } from "./handler_camera_case_to_snake";
import * as vscode from 'vscode';
import { NONAME } from "dns";



function get_string_rules(): Array<[number, string, IStringHandler]> {
    return [
        [10, "upper_case", new HandlerUpperCase()],
        [11, "lower_case", new HandlerLowerCase()],
        [12, "title_case", new HandlerTitleCase()],
        [20, 'snake_to_http_header', new HandlerSnakeCaseToHttpHeader()],
        [21, 'snake_to_camera_case', new HandlerSnakeCaseToCameraCase()],
        [30, 'http_header_to_snake', new HandlerHttpHeaderToSnakeCase()],
        [40, 'camera_case_to_snake', new HandlerCameraCaseToSnakeCase()]
    ]
}

let _default_rules = get_string_rules();

export function string_handler(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) {
    let rules = _default_rules;
    let indexLength = 2;
    let formatIndex = (index: number): string => {
        let prefix = '0'.repeat(indexLength) + index.toString()
        return prefix.slice(prefix.length - indexLength);
    }
    let items: vscode.QuickPickItem[] = [];
    for (let [index, name, handler] of rules) {
        items.push({
            'label': formatIndex(index) + "." + name,
            'description': name
        })
    }
    const selection = textEditor.selections[0];
    // vscode.Position
    const text = textEditor.document.getText(selection);

    vscode.window.showQuickPick(items).then((item) => {
        if (!item) {
            return;
        }
        let { description } = item;
        let out: string = ""
        for (let [index, name, handler] of rules) {
            if (name === description) {
                out = handler.convertStr(text);
                break;
            }
        }
        // let out = _handle_var_with_label(selected_text, description, routes);
        // update_last_used_variable(out);
        // addItemHistory({ 'label': item.description, 'description': item.description });

        let newEndPost = new vscode.Position(selection.start.line, selection.start.character + out.length);
        // let
        textEditor.edit((builder) => {
            builder.replace(selection, out);
        }).then((success) => {
            // let newPosition = new vscode.Position(startPos.line, startPos.character + replaceText.length + 1)
            textEditor.selection = new vscode.Selection(newEndPost, newEndPost);
        })

    })

}